﻿@page "/admin/liststoreinfo"

@attribute [Authorize(Roles = "Admin")]

@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="商店信息版块" />

<MDCard>
    <DataTableCard TModel="StoreInfoOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl+"list")" Title="@($"{_name}列表")" OnClickAdd="AddItem" @ref="dataTableCard">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <div style="width:70px;">
                    <MIcon Color="success"
                           Class="mr-2"
                           OnClick="()=>EditItem(context.Item)">mdi-pencil</MIcon>
                    @if (context.Item.EntryId > 0)
                    {
                        <MIcon Color="info"
                               Class="mr-2"
                               OnClick="()=>DetailItem(context.Item)">mdi-eye</MIcon>
                    }
                </div>


            }
            else if (context.Header.Value == "State")
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Stores.StateChip State="context.Item.State" />
            }
            else if (context.Header.Value == "UpdateType")
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Stores.UpdateTypeChip Type="context.Item.UpdateType" />
            }
            else if (context.Header.Value == nameof(StoreInfoOverviewModel.Name))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Users.Certifications.EntryChip Type="EntryType.Game" Id="@(context.Item.EntryId??0)" Name="@context.Item.Name" />
            }
            else if (context.Header.Value == "PlatformType")
            {
                @context.Item.PlatformType.GetDisplayName()
            }
            else if (context.Header.Value == "PriceNow")
            {
                @if (context.Item.PriceNow != null)
                {
                    @context.Item.PriceNow.Value.ToString("¥ 0.00")
                }
            }
            else if (context.Header.Value == "CutNow")
            {
                @if (context.Item.CutNow != null)
                {
                    @($"{context.Item.CutNow.Value} %")
                }
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>


<MDialog @bind-Value="_editDialog" Persistent
         MaxWidth="1200">
    <CnGalWebSite.Shared.MasaComponent.PC.Admin.Stores.EditCard Id="_editedItemId" OnEdited="OnEdited" @ref="editCard" OnClosed="OnClosed"/>
</MDialog>

@code {
    private bool _editDialog;
    private long _editedItemId;
    private bool _detailDialog;

    private string _baseUrl = "api/storeinfo/";
    private string _name = "商店信息";

    CnGalWebSite.Shared.MasaComponent.PC.Admin.Stores.EditCard editCard;

    private List<DataTableHeader<StoreInfoOverviewModel>> _headers = new List<DataTableHeader<StoreInfoOverviewModel>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(StoreInfoOverviewModel.Id)
          },
          new (){ Text= "状态", Value= "State"},
          new (){ Text= "平台类型", Value= "PlatformType"},
          new (){ Text= "平台名称", Value= nameof(StoreInfoOverviewModel.PlatformName)},
          new (){ Text= "目标", Value= nameof(StoreInfoOverviewModel.Name)},
          new (){ Text= "链接", Value= nameof(StoreInfoOverviewModel.Link)},
          new (){ Text= "现价", Value= "PriceNow"},
          new (){ Text= "折扣", Value= "CutNow"},
          new (){ Text= "更新方式", Value= "UpdateType" },
          new (){ Text= "更新时间", Value= nameof(StoreInfoOverviewModel.UpdateTime)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<StoreInfoOverviewModel> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    protected override async Task OnInitializedAsync()
    {

    }

    public async void EditItem(StoreInfoOverviewModel item)
    {
        _editedItemId = item.Id;
        _editDialog = true;
        StateHasChanged();
        if (editCard != null)
        {
            await editCard.Refresh();
        }
    }

    public void OnEdited()
    {
        _editDialog = false;
        StateHasChanged();
        if (dataTableCard != null)
        {
            dataTableCard.GetDataFromApi();
        }
    }

    public void OnClosed()
    {
        _editDialog = false;
        StateHasChanged();
    }


    public async Task AddItem()
    {
        _editedItemId = 0;
        _editDialog = true;
        StateHasChanged();
        if (editCard != null)
        {
            await editCard.Refresh();
        }
    }

    public void DetailItem(StoreInfoOverviewModel item)
    {
        NavigationManager.NavigateTo("entries/index/" + item.EntryId);
    }
}
